package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.SysMemberRole;
import com.hzhc.entity.SysYzsRole;
import com.hzhc.mapper.SysMemberRoleMapper;
import com.hzhc.mapper.SysYzsRoleMapper;
import com.hzhc.service.SysMemberRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author dongcy
 * @date 2024/4/15 9:56
 * @description SysMemberRoleServiceImpl
 */
@Service
@RequiredArgsConstructor
public class SysMemberRoleServiceImpl extends ServiceImpl<SysMemberRoleMapper, SysMemberRole> implements SysMemberRoleService {


    private final SysYzsRoleMapper roleMapper;

    private final SysMemberRoleMapper memberRoleMapper;


    @Override
    public List<SysYzsRole> getYzsRoleByMemberId(Long id) {
        return roleMapper.getYzsRoleByMemberId(id);
    }


    @Override
    public Boolean saveYzsRole(Integer userid, List<Integer> roleids) {
        // 先直接根据userid删除,很暴力
        if (userid != null) {
            SysMemberRole memberRole = new SysMemberRole();
            memberRole.setMemberId(Long.valueOf(userid));
            QueryWrapper<SysMemberRole> queryWrapper = new QueryWrapper<SysMemberRole>(memberRole);
            memberRoleMapper.delete(queryWrapper);
            for (Integer roleid : roleids) {
                SysMemberRole ur = new SysMemberRole();
                ur.setRoleId(Long.valueOf(roleid));
                ur.setMemberId(Long.valueOf(userid));
                memberRoleMapper.insert(ur);
            }
        }
        return true;
    }
}
